package tree.demo;

import tree.TreeNode;

import java.util.Stack;

/**
 * @author czt
 * @version 1.0
 * @since 2024/12/27
 *
 * 后缀表达式构建树
 */
public class Demo1 {

    public TreeNode constructExpressionTree(String[] tokens) {
        Stack<TreeNode> stack = new Stack<>();
        for (String t : tokens) {
            switch (t) {
                case "+":
                    TreeNode right = stack.pop();
                    TreeNode left = stack.pop();
                    TreeNode root = new TreeNode(Integer.parseInt(t));
                    root.left = left;
                    root.right = right;
                    stack.push(root);
                    break;
                case "-":
                    // 和+同理
                    break;
                case "*":
                    // 和-同理
                    break;
                case "/":
                    // 和*同同理
                    break;
                default:
                    stack.push(new TreeNode(Integer.parseInt(t)));
            }
        }
        return stack.peek();
    }
}
